package day_809;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class JDBCDome {
	@Test
	public void test() {
		try {
			// 加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接
			Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root",
					"123456");
			// 创建执行SQL语句
			String sql = "select * from student;";
			// 创建Statement
			Statement statement = connection.createStatement();
			// 执行SQL语句
			ResultSet ResultSet = statement.executeQuery(sql);
			
			List<Student> list = new ArrayList<Student>();
			while (ResultSet.next()) {
				Integer id = ResultSet.getInt("id");
				String name = ResultSet.getString("name");
				Integer age = ResultSet.getInt("age");
				String gender = ResultSet.getString("gender");
				Student student = new Student(id,name,age,gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
